//
//  TEMainViewController.m
//  TintExample
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEMainViewController.h"


// Thirdparty
#import "TETBXMLViewController.h"

// Utils
#import "TECacheViewController.h"
#import "TEDatabaseViewController.h"
#import "TEDatabaseObjectViewController.h"
#import "TEHTTPRequestViewController.h"
#import "TEImageLoaderViewController.h"
#import "TENAIManagerViewController.h"
#import "TEWebServiceViewController.h"
#import "TEXMLViewController.h"
#import "TEXMLObjectViewController.h"

// View
#import "TEHUDViewController.h"
#import "TEScrollViewController.h"
#import "TETabBarViewController.h"
#import "TETableViewController.h"
#import "TEViewViewController.h"

// ViewController
#import "TEWebViewController.h"

// Test
#import "TETestViewController.h"
#import "TEEdgeInsetsViewController.h"
#import "TEPropertyViewController.h"
#import "TERotateViewController.h"


@implementation TEMainViewController


- (void)dealloc {
    TTRelease(_tableView);
    TTRelease(_categories);
    [super dealloc];
}



- (void)loadView {
    [super loadView];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.autoresizingMask = TTViewAutoresizingKeepMargin;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    _categories = [[NSMutableArray alloc] init];
    TECategory *category = nil;
    
    
    
    // Thirdparty
    category = [_categories addObjectIfNotNil:[TECategory categoryWithTitle:@"Thirdparty"]];
    [category.samples addObject:[TESample sampleWithTitle:@"TBXML" viewController:[TETBXMLViewController class]]];
    
    
    
    // Utils
    category = [_categories addObjectIfNotNil:[TECategory categoryWithTitle:@"Utils"]];
    [category.samples addObject:[TESample sampleWithTitle:@"Cache" viewController:[TECacheViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"Database" viewController:[TEDatabaseViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"Database Object" viewController:[TEDatabaseObjectViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"HTTP Request" viewController:[TEHTTPRequestViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"Image Loader" viewController:[TEImageLoaderViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"NAIManager" viewController:[TENAIManagerViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"Web Service" viewController:[TEWebServiceViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"XML" viewController:[TEXMLViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"XML Object" viewController:[TEXMLObjectViewController class]]];
    
    
    
    // View
    category = [_categories addObjectIfNotNil:[TECategory categoryWithTitle:@"View"]];
    [category.samples addObject:[TESample sampleWithTitle:@"HUD" viewController:[TEHUDViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"ScrollView" viewController:[TEScrollViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"TabBar" viewController:[TETabBarViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"TableView" viewController:[TETableViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"View" viewController:[TEViewViewController class]]];
    
    
    // ViewController
    category = [_categories addObjectIfNotNil:[TECategory categoryWithTitle:@"ViewController"]];
    [category.samples addObject:[TESample sampleWithTitle:@"WebViewController" viewController:[TEWebViewController class]]];
    
    
    
    // Test
    category = [_categories addObjectIfNotNil:[TECategory categoryWithTitle:@"Test"]];
    [category.samples addObject:[TESample sampleWithTitle:@"Test" viewController:[TETestViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"EdgeInsets test" viewController:[TEEdgeInsetsViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"Property" viewController:[TEPropertyViewController class]]];
    [category.samples addObject:[TESample sampleWithTitle:@"Rotate" viewController:[TERotateViewController class]]];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_categories count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    TECategory *c = [_categories objectOrNilAtIndex:section];
    return [c.samples count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    TECategory *c = [_categories objectOrNilAtIndex:section];
    return c.title;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    
    TECategory *c = [_categories objectOrNilAtIndex:indexPath.section];
    TESample *s = [c.samples objectOrNilAtIndex:indexPath.row];
    
    cell.textLabel.text = s.title;
    
    return cell;
}




#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TECategory *c = [_categories objectOrNilAtIndex:indexPath.section];
    TESample *s = [c.samples objectOrNilAtIndex:indexPath.row];
    
    UIViewController *vc = [[s.viewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    TTRelease(vc);
}

@end




@implementation TECategory
@synthesize title = _title;
@synthesize samples = _samples;
- (id)init {
    self = [super init];
    if (self) {
        _samples = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void)dealloc {
    TTRelease(_title);
    TTRelease(_samples);
    [super dealloc];
}
+ (TECategory *)categoryWithTitle:(NSString *)title {
    TECategory *c = [[TECategory alloc] init];
    c.title = title;
    return [c autorelease];
}
@end

@implementation TESample
@synthesize title = _title;
@synthesize viewController = _viewController;
- (void)dealloc {
    TTRelease(_title);
    [super dealloc];
}
+ (TESample *)sampleWithTitle:(NSString *)title viewController:(Class)viewController {
    TESample *s = [[TESample alloc] init];
    s.title = title;
    s.viewController = viewController;
    return [s autorelease];
}
@end
